package com.vy.stl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.vy.stl.constants.Constants;
import com.vy.stl.entity.Reserve;
import com.vy.stl.service.ReserveService;
import com.vy.stl.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @create: 2025/09/02 17:16
 * @description:
 */
@RestController
@RequestMapping("/reserve")
@Slf4j
public class ReserveController {

    @Autowired
    private ReserveService reserveService;

    @GetMapping
    public Result getAll(Integer page, Integer limit, Reserve reserve) {
        /*System.out.println("page: " + page + " limit: " + limit);
        return null;*/
        if (ObjectUtil.isEmpty(page) && ObjectUtil.isEmpty(limit)) {
            List<Reserve> reserveList = reserveService.search(reserve);
            return Result.success(Constants.STATUS_200, reserveList);
        }
        return Result.success(Constants.STATUS_200, reserveService.searchByPage(page,limit,reserve));
    }

    @PostMapping
    public Result add(@RequestBody Reserve reserve) {
        /*System.out.println("reserve = " + reserve);
        return null;*/
        try{
            reserveService.add(reserve);
            return Result.success(Constants.STATUS_200);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(Constants.ADD_RESERVE_ERROR, e.getMessage());
        }
    }

    @PutMapping
    public Result modify(@RequestBody Reserve reserve) {
        try {
            reserveService.edit(reserve,false);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.EDIT_RESERVE_ERROR, e.getMessage());
        }
    }

    @PutMapping("/editStatus")
    public Result editStatus(@RequestBody Reserve reserve) {
        try {
            reserveService.editStatus(reserve);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(Constants.EDIT_RESERVE_ERROR, e.getMessage());
        }
    }

    @PutMapping("/editbatchSign")
    public Result batchUpdateStatus(@RequestBody List<Reserve> reserveList) {
        try {
            // 调用服务层处理批量更新逻辑
            reserveService.batchUpdateStatus(reserveList);
            return Result.success(Constants.STATUS_200);
        } catch (Exception e) {
            // 日志记录异常（建议使用SLF4J）
            log.error("批量处理预约状态失败", e);
            return Result.error(Constants.BATCH_EDIT_ERROR, "批量处理状态更新失败：" + e.getMessage());
        }
    }
}
